package bd;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Annuaire {
	public static ArrayList<Nucleaire> listerNucleaire(){
		ArrayList<Nucleaire> nucleaires = new ArrayList<Nucleaire>();
		Base uneBase = null;
		Nucleaire unNucleaire;
		try {			
			Connection co = Connect.getConnection();
			java.sql.Statement stmt = co.createStatement();
			ResultSet resultat = stmt.executeQuery("SELECT * from NUCLEAIRE;");
			while(resultat.next()){
				PreparedStatement stmtP = co.prepareStatement("SELECT * from BASE where id = ?;");
				stmtP.setInt(1, resultat.getInt("id_base"));
				ResultSet rs = stmtP.executeQuery();
				while(rs.next()){
					uneBase = new Base(rs.getInt("id"), rs.getString("nom"), rs.getString("ville"));
					
				}
				unNucleaire = new Nucleaire(resultat.getInt("id"), uneBase, resultat.getString("nom"), resultat.getString("pwd"));
				
				nucleaires.add(unNucleaire);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nucleaires;
	}
	public static ArrayList<Base> listerBase(){
		ArrayList<Base> bases = new ArrayList<Base>();
		Base uneBase;
		try {			
			Connection co = Connect.getConnection();
			java.sql.Statement stmt = co.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * from base;");
			while(rs.next()){
				uneBase = new Base(rs.getInt("id"), rs.getString("nom"), rs.getString("ville"));
				
				bases.add(uneBase);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bases;
	}
	public static Base trouverBase(int idBase){
		Base baseRetour = null;
		try {			
			Connection co = Connect.getConnection();
			PreparedStatement stmtP = co.prepareStatement("SELECT * from BASE where id = ?;");
			stmtP.setInt(1, idBase);
			ResultSet rs = stmtP.executeQuery();
			while(rs.next()){
				baseRetour = new Base(rs.getInt("id"), rs.getString("nom"), rs.getString("ville"));
					
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return baseRetour;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
